using UnityEngine;
using System.Collections;

public class OldManMovement : MonoBehaviour {
	
	public static OldManMovement Instance;
	
	Transform thisTransform;
	Vector3 movementSize;
	
	//multiplier
	public float EIGHT 					= 0.125F;
	public float TEN					= 0.1F;
	public float TWELVE 				= 0.083F;
	public float FIFTEEN 				= 0.067F;
	public float EIGHTEEN 				= 0.056F;
	public float TWENTY 				= 0.05F;
	
	public float MULTIPLIER				= default(float);
	
	public enum CHARACTER
	{
		PLAYER,
		ENEMY,
		DEFAULT
	};
	
	public enum MAX_TIME
	{
		NINETY							= 90,
		ONETWENTY						= 120,
		ONEFOURTYFOUR					= 144,
		ONEEIGHTY						= 180,
		TWOFORTY						= 240,
		THREEHUNDRED					= 300
	};
	
	public enum VICTORY_TIME
	{
		SIXTY							= 60,
		SEVENTYFIVE						= 75,
		NINETY							= 90,
		ONETWENTY						= 120,
		ONEFIFTY						= 150,
		ONESIXTY						= 160,
		ONEEIGHTY						= 180	
	};
	
	public CHARACTER characterType 		= CHARACTER.DEFAULT;
	
	public MAX_TIME maxTime 			= MAX_TIME.NINETY;
	public VICTORY_TIME victoryTime 	= VICTORY_TIME.SIXTY;

	public float startTime				= 0.0F;
	public float endTime 				= 0.0F;
	
	public float defaultTime 			= 0.0F;
	public float currentTime			= 0.0F;
		
	public GameObject startPosition;
	public GameObject endPosition;
	
	public Material[] animationMaterial;
	
	void Awake () 
	{
		Instance = this;
		thisTransform = transform;
	}
	
	void Start ()
	{
		//change depending on the scene
		MULTIPLIER = EIGHT;
		
		endTime = (float)maxTime;
		defaultTime = (float)victoryTime;
		
		if ( characterType == CHARACTER.ENEMY )
		{
			StartCoroutine( "MoveOldManAI" );
		}	
		
		StartCoroutine("Timer");
	}

	IEnumerator Timer ()
	{
		while( startTime < endTime )
		{
			startTime += Time.deltaTime;
			//Debug.Log(Mathf.Round( startTime ));
			yield return 0;
		}
	}
	
	public void StartMoving ()
	{
		if ( characterType == CHARACTER.PLAYER )
			StartCoroutine( "MoveOldMan" );
	}
	
	void ShowResults ()
	{
		ScreenManager.Instance.CallResultScreen();
	}
	
	Vector3 newPosition;
	float xPosition;
	
	IEnumerator MoveOldMan ()
	{
		xPosition = ( endPosition.transform.position.x - startPosition.transform.position.x ) * MULTIPLIER;
		newPosition = new Vector3( thisTransform.position.x + xPosition, thisTransform.position.y, thisTransform.position.z );
		
		//change to walk position
		this.gameObject.renderer.material = animationMaterial[1];
		
		//start walking
		while( Vector3.Distance(thisTransform.position, newPosition) > 1.0 ) //thisTransform.position != newPosition )
		{		
			thisTransform.position = Vector3.MoveTowards(thisTransform.position, newPosition, 5.0f * Time.deltaTime);
			yield return 0;
		}
		//change to idle position
		this.gameObject.renderer.material = animationMaterial[0];
	}
	
	IEnumerator MoveOldManAI ()
	{
		float distance = endPosition.transform.position.x - startPosition.transform.position.x;
		
		//randomize time for each enemyAI
		float time = Random.Range( defaultTime + 5.0f, endTime ); 	
		//compute speed depending on time and distance
		float speed = distance / time;				
		
		Vector3 newPosition = new Vector3( endPosition.transform.position.x, thisTransform.position.y, thisTransform.position.z );
		
		while( Vector3.Distance(thisTransform.position, newPosition) > 1.0  )
		{
			thisTransform.position = Vector3.MoveTowards(thisTransform.position, newPosition, speed * Time.deltaTime);
			yield return 0;
		}
	}
	
	void OnTriggerEnter ( Collider thisObject )
	{
		if ( characterType != CHARACTER.ENEMY )
		{
			if ( thisObject.tag == "Finish" )
			{
				StopCoroutine ( "Timer" );
				StopCoroutine( "MoveOldManAI" );
				StopCoroutine( "MoveOldMan" );
				currentTime = Mathf.Round( startTime ); 
	           	ShowResults ();
			}
		}
	}
	
	
}
